package com.workshop.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.base.commom.ResponseUtils;
import com.workshop.dao.UUserDao;
import com.workshop.entity.UUser;
import com.workshop.service.IUUserService;
import com.workshop.service.impl.base.BaseServiceImpl;
import com.workshop.utils.ResultMsg;
import com.workshop.utils.dto.UserDto;
import com.workshop.utils.dto.UserInfo;
import com.workshop.utils.session.SessionManager;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengluqiang
 * @since 2019-04-13
 */
@Service
public class UUserServiceImpl extends BaseServiceImpl<UUserDao, UUser> implements IUUserService {
	@Resource
	private UUserDao userDao;
	
	@Override
	public Map<String, Object> login(HttpServletRequest request, String username, String password) {
		/*// 判断手机号密码是否为空
		if(StringUtils.isBlank(username)||StringUtils.isBlank(password)) {
			return ResponseUtils.error("账号或者密码不能为空！");
		}
		
		//密码MD5加密(盐为手机号;)
		String md5Pws = Md5Utils.MD5AddSalt2(password,username);
		//判断是否存在
		UUser user = new UUser();
		user.se
		if(user==null) {
			return ResponseUtils.error("账号或者密码错误！", "user is null");
		}*/
		return null;
	}

	/**
	 * 
	 * 根据微信用户参数创建会员用户
	 */
	@Override
	public Map<String, Object> registerUserByWechat(UserDto userDto, HttpServletRequest request,
			HttpServletResponse response) {
		// 校验微信数据
		// 微信用户参数
		UserInfo userInfo = userDto.getUserInfo();
		// 只对当前公众号唯一的表示
		String openId = userDto.getOpenId();
		// 根据微信数据注册会员
		String nickName = userInfo.getNickName();
		
		
		// 通过openid查询用户数据
		UUser user = new UUser();
		user.setOpenId(openId);
		EntityWrapper<UUser> wrapper =new EntityWrapper<>(user);
		List<UUser> userList = userDao.selectList(wrapper);
		
		if (userList != null && !userList.isEmpty()) {
			user = userList.get(0);
//					if(user.getUserStatus()!=1) {
//						return ResponseUtils.error(ResultCode.USER_FROZEN);
//					}
			userDao.updateById(user);
			String key = SessionManager.saveUserBySession(request,user);
			
			return ResponseUtils.success("登录成功!", ResultMsg.USERTOKEN, key);
		}
		// 微信的性别 1=男性，2=女性，0=未知
		String gender = userInfo.getGender();
		// 微信头像
		String avatarUrl = userInfo.getAvatarUrl();
		// 微信语言
		String language = userInfo.getLanguage();
		// 微信所属国家
		String country = userInfo.getCountry();
		// 微信所在省份
		String province = userInfo.getProvince();
		// 微信所在城市
		String city = userInfo.getCity();
		
		user.setAvatarUrl(avatarUrl);
		user.setLanguage(language);
		user.setCountry(country);
		user.setProvince(province);
		user.setCity(city);
		
		// 如果gender=2 返回0(女) gender=1或0 返回1(男)
		gender = gender.equals("2") ? "0" : "1";
		user.setSex(new Integer(gender));
		user.setUserStatus(1);
		user.setLevel(0);
		
		user.setId(UUID.randomUUID().toString().replace("-", ""));
		//user.setWxId(getData());
		// 设置创建时间
		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());
		user.setNickName(nickName);
		user.setSex(1);
		user.setOpenId(openId);
		user.setUserStatus(1); //正常用户
		// 如果是微信用户授权的会员是没有账号和密码的
		userDao.insert(user);
		// 将用户数据保存到redis中并将redis中的key返回
		String key = SessionManager.saveUserBySession(request,user);
		return ResponseUtils.success("登录成功!", ResultMsg.USERTOKEN, key);
	}
}
